package com.cell.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cell.common.Result;
import com.cell.common.States;
import com.cell.common.exceptions.exceptions.RecyclableException;
import com.cell.pojo.entity.recyclable.RecyclableName;
import com.cell.pojo.entity.recyclable.RecyclableType;
import com.cell.service.recycable.IRecyclableNameService;
import com.cell.service.recycable.IRecyclableTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cloudJiang
 * @date 2022/6/21 16:33
 */

@RestController
@RequestMapping("/api.recyclable")
@Api(tags = "可回收物品获取相关接口")
public class RecyclableController {
    @Autowired
    IRecyclableTypeService recyclableTypeService;
    @Autowired
    IRecyclableNameService recyclableNameService;


    /**
     * 获取可回收物品的类别
     *
     * @return
     */
    @ApiOperation(value = "获取可回收物类别", notes = "获取可回收物类别")
    @GetMapping("/getAllType")
    Result getAllType() {
        return new Result(recyclableTypeService.getBaseMapper().selectList(null),
                States.OK.getStates(), States.OK.getDescription());
    }

    /**
     * 获取当前类别下的可回收物品
     *
     * @return
     */
    @ApiOperation(value = "获取当前类别下的所有物品", notes = "获取当前类别下的所有物品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeName", value = "类别名称", required = true)
    })
    @GetMapping("/getAllRecyclables")
    Result getAllRecyclables(@RequestParam("typeName") String typeName) {
        if (typeName==null){
            throw new RecyclableException(States.ERR_BAD_REQUEST.getStates(), "参数不能为空");
        }
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("type_name", typeName);
        Integer typeId = recyclableTypeService.getBaseMapper().selectOne(wrapper).getId();
        System.out.println(typeId);
        wrapper.clear();
        wrapper.eq("type_id", typeId);
        return new Result(recyclableNameService.getBaseMapper().selectList(wrapper),
                States.OK.getStates(), States.OK.getDescription());
    }
}
